// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#ifndef ROT_STATUS_H_
#define ROT_STATUS_H_

#include "status/module_id.h"


/**
 * Magic number used to identify when a return value is an error.
 */
#define	ROT_ERROR_MARKER			0x7f000000U

/**
 * Generate an error code for a Cerberus firmware module.
 *
 * @param module The identifier for the module that generated the error.
 * @param code The identifier for the error.
 */
#define	ROT_ERROR(module, code)		((ROT_ERROR_MARKER) | ((module) << 8) | (code))

/**
 * Boolean expression to indicate if a status return represents an error.  This is only necessary
 * for functions that return data on success.
 *
 * @param status The status return to check.
 */
#define	ROT_IS_ERROR(status)		(((status & 0xff000000) == ROT_ERROR_MARKER) || (status < 0))

/**
 * Get the module identifier from an error code.  If the error was generated by external code, the
 * original error code will be returned.
 */
#define	ROT_GET_MODULE(status)		((status < 0) ? status : ((status & 0x00ffff00) >> 8))

/**
 * Get the error identifier from an error code.  If the error was generated by external code, the
 * original error code will be returned.
 */
#define	ROT_GET_ERROR(status)		((status < 0) ? status : (status & 0x000000ff))


#endif /* ROT_STATUS_H_ */
